package com.jjh.jhwebserver.context;

import com.jjh.jhwebserver.config.ServletConfig;

public abstract class HttpServlet implements Servlet{
    @Override
    public void init(ServletConfig config) {

    }

    @Override
    public void service(JhRequset request,JhResponse respone) {

        HttpServletRequest request1 = (HttpServletRequest) request;
        HttpServletResponse respone1 = (HttpServletResponse) respone;
        String method = request1.getMethod();
        if("GET".equals(method)){
            this.doGet(request1,respone1);
        } else if ("POST".equals(method)) {
            this.doPost(request1,respone1);
        } else if ("OPTIONS".equals(method)) {
            this.doOptions(request1,respone1);
        }

    }

    @Override
    public void destroy() {

    }

    /**
     * 执行对应的get方法
     * @param request
     * @param respone
     */
    public abstract void doGet(HttpServletRequest request, HttpServletResponse respone);


    /**
     * 执行对应的post方法
     * @param request
     * @param respone
     */
    public abstract void doPost(HttpServletRequest request, HttpServletResponse respone);



    /**
     * 执行对应的options方法
     * @param request
     * @param respone
     */
    public void doOptions(HttpServletRequest request, HttpServletResponse respone){



        respone.setHeader("Access-Control-Allow-Origin:*");
        respone.setHeader("Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE");
        respone.setHeader("Access-Control-Allow-Headers: access-control-allow-origin, authority, content-type, version-info, X-Requested-With");
        respone.setHeader("Access-Control-Allow-Age:3600");
        respone.setStatus(200);
    }

}
